<template>
  <div class="home">
    <div class="title">欢乐扫雷</div>
    <div class="btn start" @click="beginClickBg(), startGame()">开始游戏</div>
    <div class="btn record" @click="beginClickBg(), $router.push('/pc/record')">游戏记录</div>
    <div class="btn setting" @click="beginClickBg(), (drawer = true)">设置</div>
    <div class="version">版本：1.1.1</div>
    <el-drawer title="游戏设置" :visible.sync="drawer" :direction="direction" size="80%">
      <div class="container">
        <div class="range inp">
          <span class="text">网格类型：</span>
          <el-select v-model="rangeValue" placeholder="请选择" @change="changeRange">
            <el-option
              v-for="item in rangeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div class="level inp">
          <span class="text">游戏难度：</span>
          <el-select v-model="levelValue" placeholder="请选择" @change="changeLevel">
            <el-option
              v-for="item in levelOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div class="failRecord inp">
          <span class="text">记录失败： </span>
          <el-switch
            v-model="isRecordFail"
            active-color="#409eff"
            inactive-color="#dcdfe6"
            @change="handleChange">
          </el-switch>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { nextTick } from "vue";
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      drawer: false,
      direction: "rtl",
      rangeOptions: [
        {
          value: 10,
          label: "10 x 10",
        },
        {
          value: 15,
          label: "15 x 15",
        },
        {
          value: 20,
          label: "20 x 20",
        },
        {
          value: 25,
          label: "25 x 25",
        },
        {
          value: 30,
          label: "30 x 30",
        },
      ],
      rangeValue: "10 x 10",
      levelOptions: [
        {
          value: 10,
          label: "10%",
        },
        {
          value: 15,
          label: "15%",
        },
        {
          value: 20,
          label: "20%",
        },
        {
          value: 25,
          label: "25%",
        },
        {
          value: 30,
          label: "30%",
        },
        {
          value: 35,
          label: "35%",
        },
        {
          value: 40,
          label: "40%",
        },
      ],
      isRecordFail: true,
      levelValue: "15%",
    };
  },
  methods: {
    ...mapMutations("setting", ["setRange", "setLevel", "setRecordFail"]),
    ...mapMutations("audio", ["beginBg", "endBg", "beginGameBg", "endGameBg", "beginClickBg"]),
    startGame() {
      this.$router.push("/pc/game");
      this.endBg();
      this.beginGameBg();
    },
    changeRange(value) {
      this.beginClickBg();
      this.setRange(value);
    },
    changeLevel(value) {
      this.beginClickBg();
      this.setLevel(value);
    },
    handleChange() {
      this.beginClickBg();
      this.setRecordFail(this.isRecordFail);
    },
    init() {
      nextTick(() => {
        this.rangeValue = this.range + " X " + this.range;
        this.levelValue = this.level + "%";
        this.isRecordFail = this.recordFail;
      });
    },
    handlePopState() {
      this.endGameBg();
      this.beginBg();
    },
  },
  computed: {
    ...mapState("setting", ["range", "level", "recordFail"]),
  },
  mounted() {
    this.init();
    window.addEventListener("popstate", this.handlePopState, false);
  },
};
</script>

<style lang="less" scoped>
.home {
  width: 1000px;
  margin: 0 auto;
  padding-top: 30px;

  .title {
    width: 200px;
    height: 50px;
    // background: cyan;
    margin: 0 auto;
    margin-bottom: 120px;
    text-align: center;
    line-height: 50px;
    font-size: 20px;
  }

  .version {
    position: absolute;
    top: 95%;
    left: 30%;
    width: 40%;
    text-align: center;
    color: #888;
  }

  .btn {
    width: 200px;
    height: 50px;
    background: #409eff;
    border-radius: 5px;
    color: #fff;
    margin: 60px auto;
    text-align: center;
    line-height: 50px;
    // font-size:20px;
    cursor: pointer;

    &:hover {
      background: #66b1ff;
    }
  }

  .container {
    // background: cyan;
    padding: 0 40px;

    .inp {
      font-size: 20px;
      margin-top: 40px;
    }
  }
}
</style>
